package com.ycl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ycl.common.BaseThread;
import com.ycl.common.Result;
import com.ycl.entity.AddressBook;
import com.ycl.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    /**
     * 新增
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseThread.getCurrentId());
        log.info("addressBook:{}",addressBook);
        addressBookService.save(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        log.info("addressBook:{}",addressBook);
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId,BaseThread.getCurrentId());
        updateWrapper.set(AddressBook::getIsDefault,0);
        // SQL: update address_book set is_default = 0 where user_id = ?
        addressBookService.update(updateWrapper);

        addressBook.setIsDefault(1);
        // SQL: update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 根据ID查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> get(@PathVariable Long id){
        log.info("id:{}",id);
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null){
            return Result.success(addressBook);
        }
        return Result.error("没有该地址");
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseThread.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        // SQL: select * from address_book where user_id = ? and is_default = 1;
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (addressBook == null){
            return Result.error("没有默认地址");
        }
        return Result.success(addressBook);
    }

    /**
     * 查询用户全部的地址信息
     */
    @GetMapping("/list")
    Result<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseThread.getCurrentId());
        log.info("addressBook:{}",addressBook);
        // 条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        // SQL: select * from address_book where user_id = ? order by update_time desc
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return Result.success(list);
    }
}
